package b2.widget.warehouse
{
	import b2.net.ServerEvent;
	import b2.net.ServerHelper;
	import b2.player.PlayerInfo;
	import b2.player.PlayerInfoManager;
	import b2.player.dataModel.Mail;
	import b2.player.dataModel.Obj;
	import b2.player.dataModel.ObjWithNum;
	import b2.widget.WidgetManager;
	import b2.widget.login.AwardMessage;
	import b2.widget.login.GiftItem;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class MailItem extends ObjectItem
	{
		public var mailId:int;
		
		public var mail:Mail;
		
		public function MailItem(mail:Mail, mailId:int)
		{
			this.mail = mail;
			this.mailId = mailId;
			super(29, 0, 0, 0, 1, 0);
		}
		
		protected override function onMouseDoubleClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var mail:Mail = this.mail;
			var awardMessage:AwardMessage;
			var giftItems:Array = new Array();
			for (var i:int = 0; i < mail.giftArray.length; i++)
			{
				var gift:ObjWithNum = mail.giftArray[i] as ObjWithNum;
				var obj:Obj = gift.obj;
				var str:String = obj.objectIndex + "," + (obj.basicLevel * 5 + obj.strengthenLevel) + ","+ gift.num
				giftItems.push(new GiftItem([str]));
			}
			awardMessage = new AwardMessage("收到来自<font color='#0000FF'> " + mail.fromName + " </font>的礼物:", "留言：" + mail.message, giftItems);
			WidgetManager.getInstance().popOut([awardMessage], false, true);
			ServerHelper.getInstance().getMail(me.id, mailId);
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_MAIL, onGetMail);
			me.mailPackage.deleteMail(mail);
		}
		
		private function onGetMail(event:Event):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.GET_MAIL, onGetMail);
			Warehouse.getInstance().refresh();
		}
	}
}